package com.tjetc.view;

import com.tjetc.domain.Admin;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;

public class AdminManagView {
    private static Scanner sc = new Scanner(System.in);
    private AdminService adminService = ClientProxy.getProxyInstance(AdminService.class);

    public void start() {
        while (true) {
            System.out.println("1.管理员列表");
            System.out.println("2.添加管理员");
            System.out.println("3.修改管理员信息");
            System.out.println("4.删除管理员");
            System.out.println("0.返回上一级");
            System.out.println("请输入你的选择0~4");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("请重新输入");
            }

        }
    }

    private void delete() {
        list();
        System.out.println("请输入将要删除的id");
        int id=sc.nextInt();
        Result result=adminService.delete(id);
        System.out.println(result.getMsg());
    }

    private void update() {
        list();
        System.out.println("请输入id");
        int id = sc.nextInt();
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        System.out.println("请输入邮箱");
        String email=sc.next();
        System.out.println("请输入手机号");
        String phone=sc.next();
        Result result=adminService.update(new Admin(id,username,password,email,phone));
        System.out.println(result.getMsg());

    }

    private void add() {
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        System.out.println("请输入邮箱");
        String email=sc.next();
        System.out.println("请输入手机号");
        String phone=sc.next();
        Result result=adminService.add(new Admin().setUsername(username).setPassword(password).setEmail(email).setPhone(phone));
        System.out.println(result.getMsg());
    }

    private void list() {
        Result result = adminService.list();
        List<Admin> list = (List<Admin>) result.getData();
        if (list.size() > 0) {
            for (Admin admin : list) {
                System.out.println(admin);
            }
        } else {
            System.out.println("没人啊");
        }
    }
}
